package tablet.module_user.fragment;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.bumptech.glide.Glide;
import com.lib.app.ARouterPathUtils;
import com.lib.app.CodeUtil;
import com.lib.fastkit.db.shared_prefrences.SharedPreferenceManager;
import com.lib.fastkit.http.ok.HttpUtils;
import com.lib.fastkit.utils.px_dp.DisplayUtil;
import com.lib.fastkit.utils.share.tool.StringUtil;
import com.lib.fastkit.views.recyclerview.tool.MyLinearLayoutManager;
import com.lib.fastkit.views.recyclerview.zhanghongyang.base.ViewHolder;
import com.lib.http.call_back.HttpNormalCallBack;
import com.lib.ui.adapter.BaseAdapter;
import com.lib.ui.fragment.BaseAppFragment;
import com.lib.utls.glide.GlideConfig;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.OnClick;
import tablet.module_user.R;
import tablet.module_user.R2;
import tablet.module_user.bean.BanJiClassBean;
import tablet.module_user.bean.UserInfoBean;

public class StudentMenuFragment_1 extends BaseAppFragment {

    @BindView(R2.id.lin_test)
    LinearLayout linTest;
    @BindView(R2.id.tv_test)
    TextView tvTest;
    @BindView(R2.id.rv_class)
    RecyclerView rvClass;

    UserInfoBean.ObjBean userInfoBean;

    private BanJiClassAdapter homeAdapter;

    private List<BanJiClassBean.ObjBean.RowsBean> classList = new ArrayList<>();

    @Override
    protected void onCreateView(View view, Bundle savedInstanceState) {

        getUserInfo();

        initView();

        initBanJiClassInfo();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_student_menu_1;
    }

    private void initBanJiClassInfo() {

        HttpUtils.with(getActivity())
                .addParam("requestType", "TABLET_LARGE_CLASS")
                .addParam("page", 0)
                .addParam("limit", 3)
                .addParam("token", SharedPreferenceManager.getInstance(getActivity()).getUserCache().getUserToken())
                .execute(new HttpNormalCallBack<BanJiClassBean>() {
                    @Override
                    public void onSuccess(BanJiClassBean result) {

                        if (result.getCode() == CodeUtil.CODE_200) {
                            if (result.getObj() != null) {

                                if (classList != null && classList.size() > 0) {
                                    classList.clear();
                                }
                                classList.addAll(result.getObj().getRows());
                                homeAdapter.notifyDataSetChanged();
                            }
                        }
                    }

                    @Override
                    public void onError(String e) {

                    }
                });

    }

    private void initView() {
        homeAdapter = new BanJiClassAdapter(getActivity(), classList);
        rvClass.setLayoutManager(new MyLinearLayoutManager(getActivity()));
        rvClass.setNestedScrollingEnabled(false);
        rvClass.setAdapter(homeAdapter);
    }

    @OnClick({R2.id.lin_test, R2.id.tv_test})
    public void onViewClicked(View view) {
        int i = view.getId();
        if (i == R.id.lin_test) {

            if (StringUtil.isNotEmpty(userInfoBean.getEvaluation_record_url(), true)) {
                ARouter.getInstance().build(ARouterPathUtils.Tablet_App_NormalDetailWebActivity)
                        .withString("urlPath", userInfoBean.getEvaluation_record_url()).navigation();
            }
        } else if (i == R.id.tv_test) {

            showToast("该功能正在建设中，敬请期待");
        }

    }


    private void getUserInfo() {

        HttpUtils.with(getActivity())
                .addParam("requestType", "UNIFIED_INTERFACE")
                .addParam("token", SharedPreferenceManager.getInstance(getActivity()).getUserCache().getUserToken())
                .execute(new HttpNormalCallBack<UserInfoBean>() {
                    @Override
                    public void onSuccess(UserInfoBean result) {

                        if (result.getCode() == CodeUtil.CODE_200) {
                            if (result.getObj() != null) {

                                userInfoBean = result.getObj();
                            }
                        }
                    }

                    @Override
                    public void onError(String e) {

                    }
                });

    }


    class BanJiClassAdapter extends BaseAdapter<BanJiClassBean.ObjBean.RowsBean> {

        public BanJiClassAdapter(Context context, List<BanJiClassBean.ObjBean.RowsBean> mData) {
            super(context, mData);
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_hudong2;
        }

        @Override
        protected void toBindViewHolder(ViewHolder holder, int position, List<BanJiClassBean.ObjBean.RowsBean> mData) {

//            holder.itemView.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    ARouter.getInstance().build(ARouterPathUtils.App_NormalDetailWebActivity)
//                            .withString("urlPath", rowsBean.getCourse_link())
//                            .navigation();
//                }
//            });

            BanJiClassBean.ObjBean.RowsBean rowsBean = mData.get(position);

            holder.setText(R.id.tv_title, rowsBean.getCourse_name());
            holder.setText(R.id.tv_remaining, "剩余名额:" + rowsBean.getRemaining());
            holder.setText(R.id.tv_ks, "课时数:" + rowsBean.getTotal_class());

            if (!"".equals(rowsBean.getPrice() + "") && null != rowsBean.getPrice() + "") {
                int money = (int) rowsBean.getPrice();
                holder.setText(R.id.tv_money, money + "");
            }

            LinearLayout lin_head = holder.getView(R.id.lin_head);
            lin_head.removeAllViews();
            final List<BanJiClassBean.ObjBean.RowsBean.TeacherInfoBean> teacherInfoBeanList = rowsBean.getTeacher_info();

            for (int i = 0; i < teacherInfoBeanList.size(); i++) {

                View view = LayoutInflater.from(getActivity()).inflate(R.layout.item_class_teacher, null);

                final ImageView iv_king = view.findViewById(R.id.iv_king);

                if (i != 0) {
                    iv_king.setVisibility(View.GONE);
                }

                final TextView tv_name = view.findViewById(R.id.tv_name);
                tv_name.setText(teacherInfoBeanList.get(i).getTeacher_name());
                final ImageView iv_head = view.findViewById(R.id.iv_head);
                final int finalI = i;
//                iv_head.setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View v) {
//                        ARouter.getInstance().build(ARouterPathUtils.App_NormalDetailWebActivity)
//                                .withString("urlPath", teacherInfoBeanList.get(finalI).getTeacher_link())
//                                .navigation();
//                    }
//                });

                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                        LinearLayout.LayoutParams.WRAP_CONTENT);
                params.setMargins(0, 0, DisplayUtil.dip2px(getActivity(), 16), 0);
                view.setLayoutParams(params);
                lin_head.addView(view);

                Glide.with(getActivity())
                        .load(teacherInfoBeanList.get(i).getTeacher_photo_url())
                        .apply(GlideConfig.getCircleOptions())
                        .into(iv_head);

            }
        }
    }
}
